package com.im323.quartz;

import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.im323.common.util.IpUtil;
import com.im323.dnspod.DnspodService;
import com.im323.dnspod.result.DnspodRecord;
import com.im323.dnspod.result.DnspodResult;

public class RecordUpdateJob implements Job {

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println("定时器执行");
		List<UpdateRecordModel> jobList = UpdateRecordModel.getJobList();
		if (jobList == null || jobList.size() <= 0) {
			return;
		}

		for (int i = 0; i < jobList.size(); i++) {
			UpdateRecordModel updateRecordModel = jobList.get(i);
			String ipAddress = IpUtil.getIpBySpeed().getIpAddress();
			if (ipAddress == null) {
				throw new RuntimeException("IP获取错误");
			}
			System.out.println("获取到IP：" + ipAddress);
			if (ipAddress.equals(updateRecordModel.getValue())) {
				System.out.println("IP无变化: 当前IP：" + ipAddress + "  上次IP：" + updateRecordModel.getValue());
				continue;
			}
			updateRecordModel.setValue(ipAddress);

			System.out.println("执行更新任务：" + updateRecordModel);
			DnspodResult<DnspodRecord> dnspodResult = DnspodService.updateDDNS(jobList.get(i));
			System.out.println("更新任务执行完成：" + dnspodResult.getStatus());
		}
	}

}
